package switchviz.gui;

import java.io.File;
import java.io.IOException;

import javax.swing.JFileChooser;

import org.jdom.JDOMException;

import switchviz.Application;
import switchviz.core.ResourceManager;
import switchviz.core.view.ViewTree;
import switchviz.recording.RecordPersistence;
import switchviz.util.FileOperations;

public class ImportDialog
{
	public static final int VIEWIMPORT = 0;
	public static final int RECORDIMPORT = 1;
	public ImportDialog(int mode)
	{
		try
		{
			switch(mode)
			{
			case VIEWIMPORT:
				initializeViewImport();
			break;
			case RECORDIMPORT:
				initializeRecordImport();
			break;
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private void initializeRecordImport() throws Exception
	{
		final JFileChooser fc = new JFileChooser();
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		int retval = fc.showOpenDialog(Application.getInstance().getMainFrame());
		if(retval == JFileChooser.APPROVE_OPTION)
		{
			File file = fc.getSelectedFile();
			
			if((new File(file, RecordPersistence.RECORDINFO + ".xml")).exists())
				FileOperations.copyDirectory(file, new File(ResourceManager.getInstance().getRecordFolder(), file.getName()));
		}
	}
	
	private void initializeViewImport() throws JDOMException, IOException, Exception
	{
		final JFileChooser fc = new JFileChooser();
		fc.setFileFilter(FileOperations.getXMLFileFilter("View Configuration File"));
		int retval = fc.showOpenDialog(Application.getInstance().getMainFrame());
		if(retval == JFileChooser.APPROVE_OPTION)
		{
			File file = fc.getSelectedFile();
			if(FileOperations.testXMLRootName(file, ViewTree.VIEWNODE))
				FileOperations.copyFile(file, new File(ResourceManager.getInstance().getViewFolder(), file.getName()));
		}
	}
}
